<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of quality_controls_controller
 *
 * @author fredy
 */
class QualityControlsController extends AppController {

    //put your code here
    var $name = "QualityControls";

    function index() {
        //Envío los datos de los predios correspondientes al proyecto seleccionado
        $id = $this->Session->read('proy_id');
        $this->layout = "ajax";
        if ($id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        } else {
            $this->QualityControl->Property->recursive = -1;
            $this->set("properties", $this->QualityControl->Property->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('Property.id', 'Property.codigo', 'Property.nombre', 'Property.vereda'))));
            $this->set('cod', $this->Session->read('cod'));
        }
    }

    function quality_index($predio) {
        $this->QualityControl->Property->recursive = -1;
        $nombre = $this->QualityControl->Property->find('first', array('conditions' => array('Property.id' => $predio), 'fields' => array('Property.nombre', 'Property.codigo')));
        $this->set('cod', $nombre['Property']['codigo'] . " " . $nombre['Property']['nombre']);
        $this->set('predio', $predio);
        $this->QualityControl->recursive = -1;
        $this->set('qualityControls', $this->QualityControl->find('all', array('conditions' => array('property_id' => $predio), 'fields' => array('QualityControl.id', 'QualityControl.observaciones', 'QualityControl.calificacion'))));
    }

    function add($predio) {
        $this->layout = "ajax";
        if (!empty($this->data)) {
            if ($this->QualityControl->saveAll($this->data)) {
                $this->Session->setFlash('Control de calidad guardado con éxito');
                $this->redirect(array('controller' => 'QualityControls', 'action' => 'quality_index', $predio));
            } else {
                $this->Session->setFlash('error guardando datos');
            }
        } else {
            $this->set('predio', $predio);
        }
    }

    function edit($predio, $id = null) {
        $this->layout = "ajax";
        if (!empty($this->data)) {
            if ($this->QualityControl->saveAll($this->data)) {
                $this->Session->setFlash('Control de calidad guardado con éxito');
                $this->redirect(array('controller' => 'QualityControls', 'action' => 'quality_index', $predio));
            } else {
                $this->Session->setFlash('error guardando datos');
            }
        } else {
            $this->data = $this->QualityControl->find("first", array("conditions" => array("QualityControl.id" => $id)));
            $this->set('predio', $predio);
        }
    }

    function delete($predio, $id) {
        if ($this->QualityControl->delete($id, true)) {
            $this->Session->setFlash('Control de calidad borrado con éxito');
            $this->redirect(array('controller' => 'QualityControls', 'action' => 'quality_index', $predio));
        }
    }

    public function report() {
        $this->layout = "csv";
        ini_set('max_execution_time', 600); //increase max_execution_time to 10 min if data set is very large
        //create a file
        $filename = "Reporte_areas_plano_topografico_" . date("Y.m.d") . ".csv";
        $csv_file = fopen('php://output', 'w');
        header('Content-type: application/csv');
        header('Content-Disposition: attachment; filename="' . $filename . '"');


        $sql = "SELECT Proyect.codigo, Property.codigo, Property.nombre, QualityControl.anexo3, QualityControl.titulos, QualityControl.poderes,
                QualityControl.lesion_enorme, QualityControl.gravamenes, QualityControl.limitaciones, QualityControl.restricciones, QualityControl.otros,
                QualityControl.otros_cual, QualityControl.subsano, QualityControl.observaciones, QualityControl.calificacion, QualityControl.documentos_requeridos
                FROM quality_controls QualityControl
                LEFT JOIN properties Property ON Property.id = QualityControl.property_id
                LEFT JOIN proyects Proyect ON Proyect.id = Property.proyect_id
                WHERE 1";
        $results = $this->QualityControl->query($sql);


        // The column headings of your .csv file   
        $header_row = array("CÓDIGO PROYECTO", "CÓDIGO PREDIO", "NOMBRE PREDIO", "ANEXO 3", "TITULOS", "PODERES", "LESIÓN ENORME", "GRAVAMENES", "LIMITACIONES", "RESTRICCIONES", "OTROS", "OTROS CUAL", "SUBSANO", "OBSERVACIONES", "CALIFICACIÓN", "DOCUMENTOS REQUERIDOS");

        fputcsv($csv_file, $header_row, ';', '"');

        foreach ($results as $result) {

            $calificacion = "";
            
            switch ($result['QualityControl']['calificacion']) {
                case 1:
                    $calificacion = "PENDIENTE";
                    break;
                case 2:
                    $calificacion = "REQUIERE DOCUMENTO";
                    break;
                case 3:
                    $calificacion = "NO CUMPLE";
                    break;
                case 4:
                    $calificacion = "CUMPLE";
                    break;
            }

            $row = array(
                $result['Proyect']['codigo'],
                $result['Property']['codigo'],
                $result['Property']['nombre'],
                $result['QualityControl']['anexo3'],
                $result['QualityControl']['titulos'],
                $result['QualityControl']['poderes'],
                $result['QualityControl']['lesion_enorme'],
                $result['QualityControl']['gravamenes'],
                $result['QualityControl']['limitaciones'],
                $result['QualityControl']['restricciones'],
                $result['QualityControl']['otros'],
                $result['QualityControl']['otros_cual'],
                $result['QualityControl']['subsano'],
                $result['QualityControl']['observaciones'],
                $calificacion,
                $result['QualityControl']['documentos_requeridos'],
            );

            fputcsv($csv_file, $row, ';', '"');
        }
        fclose($csv_file);
    }

}

?>
